package me.huqiao.loganlyzer.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件操作工具�?
 * @author hq
 *
 */
public class FileUtil {
	
	
	public static String getExtensionName(String originalFilename){
		if(originalFilename==null || originalFilename.trim().equals("")){
			return "";
		}
		return originalFilename.substring(originalFilename.lastIndexOf("."));
	}

	/**
	 * 保存文件到磁盘，如果该文件已经存在则将会被覆�?
	 * @param path
	 * @param fileName
	 * @param data
	 * @throws IOException
	 */
	public static void saveFileOnDisk(String path,String fileName,byte[] data,boolean append) throws IOException{
		File parentDir = new File(path);
		if(!parentDir.exists()){
			parentDir.mkdirs();
		}
		File file = new File(parentDir,fileName);
		if(!file.exists()){
			file.createNewFile();
		}
		saveFileOnDisk(file,data,append);
	}
	
	/**
	 * 保存文件到磁盘，如果该文件已经存在则将会被覆�?
	 * @param file
	 * @param data
	 * @throws IOException
	 */
	public static void saveFileOnDisk(File file,byte[] data,boolean append) throws IOException{
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		FileOutputStream fos = new FileOutputStream(file,append);
		fos.write(data);
		fos.close();
	}
	
	public static void saveFileOnDisk(File file,InputStream is) throws IOException{
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		FileOutputStream fos = new FileOutputStream(file,false);
		byte[] buffer = new byte[1024];
		int len = 0;
		while((len = is.read(buffer))!=-1){
			fos.write(buffer, 0, len);
		}
		fos.close();
	}
	
	/**
	 * 保存文件到磁盘，如果该文件已经存在则将会被覆�?
	 * @param file
	 * @param data
	 * @throws IOException
	 */
	public static void saveFileOnDisk(File file,String data,boolean append) throws IOException{
		saveFileOnDisk(file,data.getBytes(),append);
	}


	/**
	 * 读取MultipartFile内容为字符串
	 * @param file MultipartFile对象
	 * @return 文件内容字符串
	 * @throws IOException 如果读取过程中发生IO异常
	 */
	public static String readContentAsString(MultipartFile file) throws IOException {
		try (InputStream inputStream = file.getInputStream()) {
			// 将输入流转换为字符串
			return new String(file.getBytes(), StandardCharsets.UTF_8);
		}
	}

	/**
	 * 按行读取MultipartFile内容
	 * @param file MultipartFile对象
	 * @return 文件内容的每一行组成的列表
	 * @throws IOException 如果读取过程中发生IO异常
	 */
	public static List<String> readContentByLine(MultipartFile file) throws IOException {
		try (InputStream inputStream = file.getInputStream();
			 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {

			List<String> lines = new ArrayList<>();
			String line;
			while ((line = reader.readLine()) != null) {
				lines.add(line);
			}
			return lines;
		}
	}

	/**
	 * 按行读取普通InputStream内容
	 * @param inputStream InputStream对象
	 * @return 文件内容的每一行组成的列表
	 * @throws IOException 如果读取过程中发生IO异常
	 */
	public static List<String> readContentByLine(InputStream inputStream) throws IOException {
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {

			List<String> lines = new ArrayList<>();
			String line;
			while ((line = reader.readLine()) != null) {
				lines.add(line);
			}
			return lines;
		}
	}
	/**
	 * 读取MultipartFile内容为字节数组
	 * @param file MultipartFile对象
	 * @return 字节数组
	 * @throws IOException 如果读取过程中发生IO异常
	 */
	public static byte[] readContentAsBytes(MultipartFile file) throws IOException {
		return file.getBytes();
	}
}
